package cn.edu.neusoft.wuguangsheng.flowerimpression.activity;

import android.support.v4.app.Fragment;
import android.support.v4.app.FragmentManager;
import android.support.v4.app.FragmentTransaction;
import android.view.View;
import android.widget.ImageView;
import android.widget.LinearLayout;
import android.widget.TextView;

import cn.edu.neusoft.wuguangsheng.flowerimpression.R;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.ShopGouWCFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.ShopShouCFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.ShopShouYFragment;
import cn.edu.neusoft.wuguangsheng.flowerimpression.fragment.ShopWodFragment;

public class ShopMainActivity extends BaseActivity implements View.OnClickListener {

    LinearLayout tab_shop_sy;
    LinearLayout tab_shop_sc;
    LinearLayout tab_shop_gwc;
    LinearLayout tab_shop_wd;

    ImageView img_shop_sy;
    ImageView img_shop_sc;
    ImageView img_shop_gwc;
    ImageView img_shop_wd;

    TextView tv_shop_sy;
    TextView tv_shop_sc;
    TextView tv_shop_gwc;
    TextView tv_shop_wd;

    Fragment fragment_shop_sy;
    Fragment fragment_shop_sc;
    Fragment fragment_shop_gwc;
    Fragment fragment_shop_wd;

    FragmentManager fragmentManager1;
    FragmentTransaction fragmentTransaction1;

    @Override
    void initView() {
        setLayout_file(R.layout.activity_shop_main);

        tab_shop_sy = (LinearLayout)findViewById(R.id.tab_shop_sy);
        tab_shop_sc = (LinearLayout)findViewById(R.id.tab_shop_sc);
        tab_shop_gwc = (LinearLayout)findViewById(R.id.tab_shop_gwc);
        tab_shop_wd = (LinearLayout)findViewById(R.id.tab_shop_wd);


        img_shop_sy = (ImageView)findViewById(R.id.img_shop_sy);
        img_shop_sc = (ImageView)findViewById(R.id.img_shop_sc);
        img_shop_gwc = (ImageView)findViewById(R.id.img_shop_gwc);
        img_shop_wd = (ImageView)findViewById(R.id.img_shop_wd);

        tv_shop_sy = (TextView)findViewById(R.id.tv_shop_sy);
        tv_shop_sc = (TextView)findViewById(R.id.tv_shop_sc);
        tv_shop_gwc = (TextView)findViewById(R.id.tv_shop_gwc);
        tv_shop_wd = (TextView)findViewById(R.id.tv_shop_wd);

        selectTab(0);

    }

    @Override
    void initEvent() {
        tab_shop_sy.setOnClickListener(this);
        tab_shop_sc.setOnClickListener(this);
        tab_shop_gwc.setOnClickListener(this);
        tab_shop_wd.setOnClickListener(this);

    }

    @Override
    void initDate() {

    }

    private void selectTab(int i) {
        fragmentManager1 = getSupportFragmentManager();
        fragmentTransaction1 = fragmentManager1.beginTransaction();
        hideFragment(fragmentTransaction1);
        switch (i) {
            case 0:
                if (fragment_shop_sy == null) {
                    fragment_shop_sy = new ShopShouYFragment();
                    fragmentTransaction1.add(R.id.shopcontent, fragment_shop_sy);
                } else {
                    fragmentTransaction1.show(fragment_shop_sy);
                }
                //tv_px.setTextColor(0x2c2c2cdd);
                img_shop_sy.setImageResource(R.mipmap.s_dpl);

                break;
            case 1:
                if (fragment_shop_sc == null) {
                    fragment_shop_sc = new ShopShouCFragment();
                    fragmentTransaction1.add(R.id.shopcontent, fragment_shop_sc);
                } else {

                    fragmentTransaction1.show(fragment_shop_sc);
                }
                //tv_jlb.setTextColor(0x2c2c2cdd);
                img_shop_sc.setImageResource(R.mipmap.s_scl);
                break;
            case 2:
                if (fragment_shop_gwc == null) {
                    fragment_shop_gwc = new ShopGouWCFragment();
                    fragmentTransaction1.add(R.id.shopcontent, fragment_shop_gwc);
                } else {
                    fragmentTransaction1.show(fragment_shop_gwc);
                }
                img_shop_gwc.setImageResource(R.mipmap.s_gwcl);
                //tv_lm.setTextColor(0x2c2c2cdd);
                break;

            case 3:
                if (fragment_shop_wd == null) {
                    fragment_shop_wd = new ShopWodFragment();
                    fragmentTransaction1.add(R.id.shopcontent, fragment_shop_wd);
                } else {
                    fragmentTransaction1.show(fragment_shop_wd);
                }
                //tv_st.setTextColor(0x2c2c2cdd);
                img_shop_wd.setImageResource(R.mipmap.s_wdl);
                break;

            default:
                break;
        }
        fragmentTransaction1.commit();
    }

    private void resetImage() {
        img_shop_sy.setImageResource(R.mipmap.s_dph);
        img_shop_sc.setImageResource(R.mipmap.s_sch);
        img_shop_gwc.setImageResource(R.mipmap.s_gwch);
        img_shop_wd.setImageResource(R.mipmap.s_wdh);
    }
    private void hideFragment(FragmentTransaction fragmentTransaction) {
        if (fragment_shop_sy != null) {
            fragmentTransaction.hide(fragment_shop_sy);
        }
        if (fragment_shop_sc != null) {
            fragmentTransaction.hide(fragment_shop_sc);
        }
        if (fragment_shop_gwc != null) {
            fragmentTransaction.hide(fragment_shop_gwc);
        }
        if (fragment_shop_wd != null) {
            fragmentTransaction.hide(fragment_shop_wd);
        }
    }

    @Override
    public void onClick(View view) {
        resetImage();
        switch (view.getId()) {
            case R.id.tab_shop_sy:
                selectTab(0);
                break;
            case R.id.tab_shop_sc:
                selectTab(1);
                break;
            case R.id.tab_shop_gwc:
                selectTab(2);
                break;
            case R.id.tab_shop_wd:
                selectTab(3);
                break;
            default:
                break;
        }

    }
}
